#!/usr/bin/env python
'''
Copyright (C) 2006, 2007 Aaron Spike, aaron@ekips.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''

import pygame
from pygame.locals import *
from activity import *

class Crayon(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) 
        self.ptlist = [(12,3),(3,7),(11,68),(4,77),(15,99),(77,70),(66,47),(54,47)]
        self.endlist = [(138,355),(150,380),(167,380),(182,376),(196,368),(211,351),(199,325)]
        self.sounds = {}
        self.changed = False
        self.cover, self.rect = load_image('crayon.png')
        self.image, self.rect = load_image('crayon.png')
        self.init_colors()
    def init_colors(self):
        self.index = 0
        self.colors = [ 
                ((255,0,0),'red'),
                ((245,121,0),'orange'),
                ((255,255,0),'yellow'),
                ((0,200,0),'green'),
                ((0,0,255),'blue'),
                ((128,0,128),'purple'),
                ((80,60,20),'brown'),
                ((0,0,0),'black'),
                ((128,128,128),'gray'),
                ((254,254,254),'white'),
                ((255,0,128),'pink')
            ]
        self.change(0)
        
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.topleft = pos
        self.changed = False

    def change(self, delta):
        self.index = self.index + delta
        if self.index < 0:
            self.index = len(self.colors) - 1
        elif self.index >= len(self.colors):
            self.index = 0
        pygame.draw.polygon(self.image, self.color(),self.ptlist)
        pygame.draw.polygon(self.image, self.color(),self.endlist)
        self.image.blit(self.cover,(0,0))
        self.sounds.setdefault(self.name(),load_sound('%s.ogg' % self.name())).play()
        self.changed = True

    def up(self):
        self.change(1)
    def down(self):
        self.change(-1)
    def color(self):
        return self.colors[self.index][0]
    def name(self):
        return self.colors[self.index][1]
    

class CrayonActivity(Activity):
    def __init__(self, screen):
        Activity.__init__(self, screen)
        self.crayon = Crayon()
        self.allsprites = pygame.sprite.RenderPlain((self.crayon))
        self.pos = None
    def line(self, surf, begin, end, color):
        size = 20
        pygame.draw.circle(surf, color, begin, size-1)
        pygame.draw.line(surf, color, begin, end, 2*size)
        pygame.draw.circle(surf, color, end, size-1)
    def handle_events(self):
        event = pygame.event.wait()
        if event.type == QUIT:
            self.quit = True
            return
        elif event.type == KEYUP:
            self.changed = True
            if event.key == K_ESCAPE:
                self.quit = True
                return
            elif event.key == K_n and KMOD_CTRL & event.mod:
                self.background.fill((0,0,0))
                self.changed = True
        elif event.type == MOUSEBUTTONUP:
            if event.button == 1:
                self.crayon.up()
            elif event.button == 3:
                self.crayon.down()
            self.changed = True
        self.pos = pygame.mouse.get_pos()
        if self.pos != self.mprev:
            self.changed = True
    def on_change(self):
        if self.pos != self.mprev:
            if self.mprev:
                self.line(self.background, self.mprev, self.pos, self.crayon.color())
            self.mprev = self.pos
        self.allsprites.update()
        self.screen.blit(self.background, (0, 0))
        self.allsprites.draw(self.screen)
            